package com.appkubes.mall.admin.application.push.controller;

import com.alibaba.fastjson.JSONObject;
import com.appkubes.base.core.entity.vo.PageVO;
import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.push.entity.dto.PushDTO;
import com.appkubes.mall.admin.application.push.entity.vo.PushVO;
import com.appkubes.mall.admin.application.user.user.service.UserService;
import com.appkubes.mall.admin.utils.BaseResponse;
import com.appkubes.mall.admin.utils.OkHttpCli;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "消息推送")
@RestController
@RequestMapping("/push")
public class PushController {

    @Resource
    UserService userService;

    @Autowired
    private OkHttpCli okHttpCli;

    @ApiOperation(value = "添加推送")
    @PostMapping("/add")
    public Response loginByWxApp(@ParamValid PushVO pushVO) {
        String url = "http://service-portal-admin.youxuanan.com/app/push/add";
        String cid = "";
        if (!pushVO.getTel().equals("")) {
            cid = userService.queryCidByTel(pushVO.getTel());
            if (cid == null) {
                return new Response(-1,"该用户未绑定CID，或绑定CID失败");
            }
        }
        Map params = new HashMap();
        params.put("title", pushVO.getTitle());
        params.put("content", pushVO.getContent());
        params.put("type", pushVO.getType());
        params.put("param", pushVO.getParam());
        params.put("cid", cid);
        String message = okHttpCli.doPost(url, params);

        BaseResponse<List<PushDTO>> clazz = JSONObject.parseObject(message, BaseResponse.class);

        Response response = new Response();
        BeanUtils.copyProperties(clazz, response);
        return response;
    }

    @ApiOperation(value = "推送列表")
    @PostMapping("/list")
    public Response list(@ParamValid PageVO pageVO) {
        String url = "http://service-portal-admin.youxuanan.com/app/push/get";
        Map params = new HashMap();
        params.put("page", pageVO.getPage().toString());
        params.put("size", pageVO.getSize().toString());
        String message = okHttpCli.doGet(url, params);

        BaseResponse<List<PushDTO>> clazz = JSONObject.parseObject(message, BaseResponse.class);

        Response response = new Response();
        BeanUtils.copyProperties(clazz, response);
        return response;
    }


}
